package it.cast.wechat.service.ServiceImpl;

import it.cast.wechat.mapper.AddressMapper;
import it.cast.wechat.pojo.Address;
import it.cast.wechat.pojo.AddressExample;
import it.cast.wechat.service.AddressService;
import it.cast.wechat.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;
    @Override
    public List<Address> queryAddressByUserId(String userId) {
        AddressExample example = new AddressExample();
        AddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);

        List<Address> addresses = addressMapper.selectByExample(example);
        return addresses;
    }

    @Override
    public Address queryAddressByAddressId(String addressId) {
        return addressMapper.selectByPrimaryKey(addressId);
    }

    @Override
    public int addressModify(Address address) {
        //添加
        int state = 0;
        if (StringUtils.isEmpty(address.getAddressId())){
            address.setAddressId(CommonUtils.uuid());
            state = addressMapper.insert(address);
        }else {
            state = addressMapper.updateByPrimaryKey(address);
        }
        return state;
    }

    @Override
    public void deleteAddressById(String addressId) {
        addressMapper.deleteByPrimaryKey(addressId);
    }
}
